#include <gtest/gtest.h>
#include "../utils/test_utils.h"

using namespace TestUtils;

class PrebuiltLibsTest : public ::testing::Test {
protected:
    void SetUp() override {
        cleanupBuildOutputs();
    }
    
    void TearDown() override {
        cleanupBuildOutputs();
    }
};

TEST_F(PrebuiltLibsTest, PrebuiltLibraryBuild) {
    TestScope scope("PrebuiltLibraryBuild");
    
    // 构建预编译库测试目标
    std::string command = Commands::BUILD_TARGET + " " + Targets::PREBUILT_TEST;
    auto result = executeBuildCommand(command);
    
    // 验证构建成功
    EXPECT_EQ(result.exitCode, 0) << "Prebuilt library build failed: " << result.output;
    EXPECT_TRUE(result.containsOutput("Build completed successfully")) 
        << "Build success message not found";
    
    // 验证目标可执行文件生成
    std::string targetPath = Paths::OUT_RELEASE + "/prebuilt_test";
    EXPECT_TRUE(fileExists(targetPath)) 
        << "Prebuilt test executable not found: " << targetPath;
}

TEST_F(PrebuiltLibsTest, PrebuiltLibraryFiles) {
    TestScope scope("PrebuiltLibraryFiles");
    
    // 验证预编译库文件存在
    std::string prebuiltDir = "examples/prebuilt_lib_test";
    EXPECT_TRUE(directoryExists(prebuiltDir)) 
        << "Prebuilt test directory not found";
    
    // 检查预编译库相关文件
    std::string libDir = prebuiltDir + "/lib";
    EXPECT_TRUE(directoryExists(libDir)) 
        << "Prebuilt lib directory not found";
    
    // 检查预编译对象文件
    std::string objFile = prebuiltDir + "/prebuilt_math.o";
    EXPECT_TRUE(fileExists(objFile)) 
        << "Prebuilt object file not found: " << objFile;
}

TEST_F(PrebuiltLibsTest, PrebuiltLibraryRuntime) {
    TestScope scope("PrebuiltLibraryRuntime");
    
    // 先构建预编译库测试
    std::string buildCommand = Commands::BUILD_TARGET + " " + Targets::PREBUILT_TEST;
    auto buildResult = executeBuildCommand(buildCommand);
    ASSERT_EQ(buildResult.exitCode, 0) << "Build failed";
    
    // 运行可执行文件测试预编译库功能
    std::string targetPath = Paths::OUT_RELEASE + "/prebuilt_test";
    ASSERT_TRUE(fileExists(targetPath)) << "Executable not found";
    
    auto runResult = executeBuildCommand(targetPath);
    EXPECT_EQ(runResult.exitCode, 0) 
        << "Prebuilt test executable failed to run: " << runResult.output;
    
    // 验证预编译库功能正常工作
    // 这里的具体验证取决于prebuilt_test程序的输出
    EXPECT_FALSE(runResult.output.empty()) 
        << "Prebuilt test should produce some output";
}

TEST_F(PrebuiltLibsTest, DynamicLibraryCopy) {
    TestScope scope("DynamicLibraryCopy");
    
    // 执行完整构建以触发动态库拷贝
    auto result = executeBuildCommand(Commands::BUILD_ALL);
    ASSERT_EQ(result.exitCode, 0) << "Build failed";
    
    // 验证动态库被拷贝到发布目录
    std::vector<std::string> expectedLibs = {
        "libutils.so",
        "libprebuilt_graphics.so"
    };
    
    for (const auto& lib : expectedLibs) {
        std::string libPath = Paths::OUT_RELEASE + "/" + lib;
        EXPECT_TRUE(fileExists(libPath)) 
            << "Dynamic library not copied: " << libPath;
    }
}

TEST_F(PrebuiltLibsTest, PrebuiltLibraryDependencies) {
    TestScope scope("PrebuiltLibraryDependencies");
    
    // 构建预编译库测试，验证依赖解析
    auto result = executeBuildCommand(Commands::BUILD_TARGET + " " + Targets::PREBUILT_TEST);
    
    EXPECT_EQ(result.exitCode, 0) << "Build failed";
    
    // 验证依赖分析阶段包含预编译库信息
    EXPECT_TRUE(result.containsOutput("Phase3 -- Dependency analysis")) 
        << "Dependency analysis phase not found";
    
    // 验证预编译库在构建过程中被正确处理
    EXPECT_TRUE(result.containsOutput("Phase4 -- Build execution")) 
        << "Build execution phase not found";
}

TEST_F(PrebuiltLibsTest, PrebuiltLibraryLinking) {
    TestScope scope("PrebuiltLibraryLinking");
    
    // 构建使用预编译库的目标
    auto result = executeBuildCommand(Commands::BUILD_TARGET + " " + Targets::PREBUILT_TEST);
    
    EXPECT_EQ(result.exitCode, 0) << "Linking with prebuilt library failed: " << result.output;
    
    // 验证链接过程没有错误
    EXPECT_FALSE(result.containsOutput("undefined reference")) 
        << "Undefined reference errors found: " << result.output;
    EXPECT_FALSE(result.containsOutput("cannot find")) 
        << "Library not found errors: " << result.output;
}

TEST_F(PrebuiltLibsTest, PrebuiltLibraryIncludePaths) {
    TestScope scope("PrebuiltLibraryIncludePaths");
    
    // 验证预编译库的头文件目录存在
    std::string includeDir = "examples/prebuilt_lib_test/include";
    EXPECT_TRUE(directoryExists(includeDir)) 
        << "Prebuilt library include directory not found: " << includeDir;
    
    // 查找头文件
    auto headerFiles = findFiles(includeDir, ".h");
    EXPECT_FALSE(headerFiles.empty()) 
        << "No header files found in prebuilt library include directory";
}

TEST_F(PrebuiltLibsTest, PrebuiltLibraryModuleConfiguration) {
    TestScope scope("PrebuiltLibraryModuleConfiguration");
    
    // 验证预编译库测试的Module文件存在且配置正确
    std::string modulePath = "examples/prebuilt_lib_test/Module";
    EXPECT_TRUE(fileExists(modulePath)) 
        << "Prebuilt library Module file not found: " << modulePath;
    
    // 读取Module文件内容
    std::string moduleContent = readFile(modulePath);
    EXPECT_FALSE(moduleContent.empty()) << "Module file is empty";
    
    // 验证Module文件包含预编译库相关配置
    EXPECT_TRUE(containsString(moduleContent, "AddExecutable") || 
                containsString(moduleContent, "prebuilt")) 
        << "Module file should contain prebuilt library configuration";
}